<template>
  <div class="daily-container">
    <!-- 搜索条件 -->
    <el-form :inline="true" :model="searchForm" class="search-form">
      <el-form-item label="日期">
        <el-date-picker
          v-model="searchForm.date"
          type="date"
          placeholder="选择日期"
        />
      </el-form-item>
      <el-form-item label="采集点">
        <el-select v-model="searchForm.pointId" placeholder="请选择采集点">
          <el-option
            v-for="item in pointOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="handleSearch">查询</el-button>
        <el-button @click="handleReset">重置</el-button>
        <el-button type="success" @click="handleExport">导出</el-button>
      </el-form-item>
    </el-form>

    <!-- 数据概览 -->
    <el-row :gutter="20" class="data-overview">
      <el-col :span="6" v-for="item in overviewData" :key="item.label">
        <el-card class="overview-card">
          <div class="label">{{ item.label }}</div>
          <div class="value">{{ item.value }}</div>
          <div class="unit">{{ item.unit }}</div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 图表展示 -->
    <el-card class="chart-card">
      <template #header>
        <div class="card-header">
          <span>24小时能耗曲线</span>
        </div>
      </template>
      <div class="chart" ref="chartRef"></div>
    </el-card>

    <!-- 数据表格 -->
    <el-card class="table-card">
      <template #header>
        <div class="card-header">
          <span>小时数据</span>
        </div>
      </template>
      <el-table
        v-loading="loading"
        :data="tableData"
        border
        style="width: 100%"
      >
        <el-table-column prop="hour" label="时间" width="100">
          <template #default="{ row }">
            {{ row.hour }}:00
          </template>
        </el-table-column>
        <el-table-column prop="value" label="能耗值">
          <template #default="{ row }">
            {{ row.value }} {{ row.unit }}
          </template>
        </el-table-column>
        <el-table-column prop="peak" label="峰谷类型" />
        <el-table-column prop="cost" label="电费">
          <template #default="{ row }">
            ¥{{ row.cost }}
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import { ref, reactive, onMounted, onUnmounted } from 'vue'
import * as echarts from 'echarts'
import type { ECharts } from 'echarts'

// 搜索表单
const searchForm = reactive({
  date: new Date(),
  pointId: ''
})

// 采集点选项
const pointOptions = ref([
  { label: '采集点1', value: '1' },
  { label: '采集点2', value: '2' }
])

// 数据概览
const overviewData = ref([
  { label: '日总能耗', value: '1,234.56', unit: 'kWh' },
  { label: '最大负荷', value: '89.12', unit: 'kW' },
  { label: '最小负荷', value: '23.45', unit: 'kW' },
  { label: '日电费', value: '678.90', unit: '元' }
])

// 图表相关
const chartRef = ref<HTMLElement>()
let chart: ECharts | null = null

// 表格数据
const loading = ref(false)
const tableData = ref([
  {
    hour: '00',
    value: 45.67,
    unit: 'kWh',
    peak: '谷时',
    cost: 23.45
  }
])

// 初始化图表
const initChart = () => {
  if (!chartRef.value) return
  chart = echarts.init(chartRef.value)
  
  const option = {
    tooltip: {
      trigger: 'axis'
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true
    },
    xAxis: {
      type: 'category',
      data: Array.from({ length: 24 }, (_, i) => `${i}:00`)
    },
    yAxis: {
      type: 'value',
      name: 'kWh'
    },
    series: [
      {
        name: '能耗',
        type: 'line',
        smooth: true,
        data: Array.from({ length: 24 }, () => Math.random() * 100),
        itemStyle: {
          color: '#409EFF'
        },
        areaStyle: {
          color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [
            { offset: 0, color: 'rgba(64,158,255,0.3)' },
            { offset: 1, color: 'rgba(64,158,255,0.1)' }
          ])
        }
      }
    ]
  }
  
  chart.setOption(option)
}

// 搜索
const handleSearch = () => {
  // TODO: 实现搜索逻辑
}

// 重置
const handleReset = () => {
  searchForm.date = new Date()
  searchForm.pointId = ''
  handleSearch()
}

// 导出
const handleExport = () => {
  // TODO: 实现导出逻辑
}

// 监听窗口大小变化
const handleResize = () => {
  chart?.resize()
}

onMounted(() => {
  initChart()
  window.addEventListener('resize', handleResize)
})

onUnmounted(() => {
  window.removeEventListener('resize', handleResize)
  chart?.dispose()
})
</script>

<style lang="scss" scoped>
.daily-container {
  padding: 20px;
  
  .search-form {
    margin-bottom: 20px;
  }
  
  .data-overview {
    margin-bottom: 20px;
    
    .overview-card {
      background: rgba(255, 255, 255, 0.05);
      border: none;
      
      .label {
        font-size: $font-size-small;
        color: $text-color-secondary;
        margin-bottom: 8px;
      }
      
      .value {
        font-size: 24px;
        font-weight: bold;
        color: $text-color;
        margin-bottom: 4px;
      }
      
      .unit {
        font-size: $font-size-small;
        color: $text-color-secondary;
      }
    }
  }
  
  .chart-card,
  .table-card {
    background: rgba(255, 255, 255, 0.05);
    border: none;
    margin-bottom: 20px;
    
    .card-header {
      display: flex;
      justify-content: space-between;
      align-items: center;
    }
    
    .chart {
      height: 400px;
    }
  }
}
</style>
